<?php

namespace Biz\model\wiki;

use Doraemon\tools\ImgUrl;


class WikiBrandArea extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_store';

    const _DATABASE_ = 'store';

    const _TABLE_NAME = 'wiki_brand_area';

    const STATUS_NORMAL = 1;
    const STATUS_DELETE = 9;
    const SHOW_TYPE_WHOLE = 1;//完整展示
    const SHOW_TYPE_PART = 2;//部分展示
    const ADM_SHOW_TYPE_MAP = [
        self::SHOW_TYPE_WHOLE => '完整展示',
        self::SHOW_TYPE_PART  => '部分展示',
    ];

    public static function admGetInfo($brand_id)
    {
        $where = [
            'brand_id' => $brand_id,
            'brand_area_status' => self::STATUS_NORMAL,
        ];
        return self::get('*', $where);
    }

    public static function admSaveInfo($brand_id, $data)
    {
        if (self::admGetInfo($brand_id)) {
            $where = [
                'brand_id' => $brand_id,
                'brand_area_status' => self::STATUS_NORMAL,
            ];
            $data['edittime'] = date("Y-m-d H:i:s");
            return self::update($data, $where)->rowCount();
        } else {
            $data['brand_id'] = $brand_id;
            $data['addtime'] = date("Y-m-d H:i:s");
            self::insert($data);
            return self::id();
        }
    }


    public static function getAllByWhere($where = [],$files = '*')
    {
        $data = self::select($files, $where);
        if(!empty($data)) {
            $data = array_map(function ($_data) {
                return self::_filter($_data);
            }, $data);
        }
        return $data;
    }

    public static function _filter($data){
        foreach($data as $_k=>&$_v){
            switch ($_k){
                case 'bg_pic_id':
                    $data['bg_pic_url'] = !empty($_v) ? ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $_v, ImgUrl::IMAGE_SIZE_OP_ORI) : '';
                    break;
                case 'share_pic_id':
                    $data['share_pic_url'] = !empty($_v) ? ImgUrl::getUrl(ImgUrl::APPTYPE_BIZ, $_v, ImgUrl::IMAGE_SIZE_SQ_PHONE) : '';
                    break;
            }
        }
        return $data;
    }

    public static function getOneByWhere( $where = [], $files = '*')
    {
        $data = self::get($files, $where);
        $datas[0] = $data;
        if(!empty($data)) {
            $datas = array_map(function ($_data) {
                return self::_filter($_data);
            }, $datas);
        }
        return $datas[0];
    }

    public static function addData($data)
    {
        try {
            self::insert($data);
            return self::id();
        } catch (Exception $e) {
            return false;
        }
    }

    public static function updateData($data, $where)
    {
        try {
            self::update($data, $where);
            return true;
        } catch (Exception $e) {
            return false;
        }
    }
}